package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    // 注解包括权限的名称，包含权限的表达式
    @RequiredPermission({"部门列表","department:list"})
    public String listAll(Model model, QueryObject qo){
        PageResult pageResult = departmentService.query(qo);
        model.addAttribute("pageResult",pageResult);
        //把载具里面的数据传递给用户看
        //请求转发
        //return "forward:/WEB-INF/views/department/list.jsp";
        return "department/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission({"部门删除","department:delete"})
    public String delete(Long id){
        departmentService.delete(id);
        //重定向,重新定位到一个新的请求路径
        return "redirect:/department/list";
    }


    //跳转到input页面
    @RequestMapping("/input")
    public String input(Model model,Long id){
        if (id != null) {
            Department one = departmentService.getOne(id);
            model.addAttribute("dept",one);
        }
        //请求转发
        return "department/input";
    }


    //跳转到input页面之后,填完/修改完数据信息之后,点击保存按钮
    //把数据保存到数据库,然后重定向到 department/list 请求路径,相当于又帮我们查了一次
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"部门新增修改","department:saveUpdate"})
    public String saveOrUpdate(Department department){
        departmentService.saveOrUpdate(department);
        //保存/修改之后, 重定向到department/list请求路径,相当于重新查询了一次最新的数据
        return "redirect:/department/list";
    }
}
